//
// Created by YueChuan on 2023/5/4.
//

#ifndef YUECHUAN_SOCKET_STREAM_H
#define YUECHUAN_SOCKET_STREAM_H

#include "socket.h"
#include "mutex.h"
#include "iomanager.h"
#include "stream.h"

namespace yuechuan {
    class SocketStream : public Stream {
    public:
        typedef std::shared_ptr<SocketStream> ptr;

        SocketStream(Socket::ptr sock, bool owner = true);

        ~SocketStream();

        virtual int read(void *buffer, size_t length) override;

        virtual int read(ByteArray::ptr buffer, size_t length) override;

        virtual int write(const void *buffer, size_t length) override;

        virtual int write(ByteArray::ptr buffer, size_t lenght) override;

        virtual void close() override;

        Socket::ptr getSocket() const { return m_sock; }

        bool isConnected() const;

        Address::ptr getRemoteAddress();
        Address::ptr getLocalAddress();
        std::string getRemoteAddressString();
        std::string getLocalAddressString();

    private:
        /// Socket 类
        Socket::ptr m_sock;
        /// 是否主控
        bool m_owner;
    };
}


#endif //YUECHUAN_SOCKET_STREAM_H
